/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.group.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;


import net.comm.group.service.impl.GAlbumServiceImpl;
import net.comm.group.web.form.GAlbumForm;
import net.comm.group.web.form.GForumForm;
import net.comm.group.web.form.GPictureForm;
import net.comm.util.HtmlMessageBox;
import net.comm.web.base.AbstractBaseAction;


/**
 *群相册Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GAlbumAction extends AbstractBaseAction {
	
	private GAlbumServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String LIST_FORWARD = "list";
	final String ADLIST_FORWARD = "adlist";
	final String OPER_FORWARD = "oper";
	final String LOGIN_FORWARD = "loginFwd";
	
	
	/**
	 *构造方法
	 */
	public GAlbumAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new GAlbumServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(LIST_FORWARD);
		} else {
			return null;
		}
	}
	
	/**
	 * 查询群相册清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GAlbumForm theForm = (GAlbumForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 * 设置当前页面key和上页lastKey
		 */
		theForm.setLastKey(getReqParam(request, KEY));
		/**
		 *调用services中的方法
		 */
		List list = service.list(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("albumlist" , list);
		request.setAttribute("theForm", theForm);

		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	
	/**
	 * 查询群相册清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward adlist(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GAlbumForm theForm = (GAlbumForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 * 设置当前页面key和上页lastKey
		 */
		theForm.setLastKey(getReqParam(request, KEY));
		/**
		 *调用services中的方法
		 */
		List list = service.adlist(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("albumlist" , list);
		request.setAttribute("theForm", theForm);

		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	
	/**
	 *添加群相册
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GAlbumForm theForm = (GAlbumForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		String path = (String)request.getSession().getServletContext().getRealPath("/");
		
		service.add(theForm,path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(theForm.getGroupID());
		theForm.setQryUserID(theForm.getUserID());
		
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping, form, request, response);

	}
	
	/**
	 * 跳转到显示页面
	 */
	public ActionForward addPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
		GAlbumForm theForm = (GAlbumForm) form;
	
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
		if(userID == null || userID.equals("")){
			
			return mapping.findForward(LOGIN_FORWARD);
		}
		theForm.setUserID(userID);		
		theForm.setProtect(GlbFinal.USERSET_MEMBER);
		theForm.setDelflg(GlbFinal.YES);
		
		request.setAttribute("theForm", theForm);

		return mapping.findForward(OPER_FORWARD);
	}	
	/**
	 * 跳转到明细页面
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
		 	 					HttpServletRequest request , 
		 	 					HttpServletResponse response) 
		throws Exception
	{

		GAlbumForm theForm = (GAlbumForm) form;
		/**
		 *定义数据封装类保存提交的数据
		 */
		ReturnMessage msg  = new ReturnMessage();
		
		service.get(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			/**
			 * 出现错误,则调用如下方法把错误信息提示到客户端,该部分
			 * 是使用response.write方法把相关的html代码写到客户端浏览器
			 * 1.hmb.setAlertType 提示类型(对话框,页面.....)
			 * 2.hmb.setPrintMessage 提示显示的信息
			 * 3.hmb.setForwardUrl 提示后跳转的URL
			 */
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
	
		theForm.setAction(getReqParam(request, "action"));
		
		request.setAttribute("theForm" , theForm);
		
		return mapping.findForward(OPER_FORWARD);
	}	
	
	/**
	 * 跳转到编辑页面
	 */
	public ActionForward modifyPage(ActionMapping mapping, ActionForm form,
							 	    HttpServletRequest request , 
							 	    HttpServletResponse response)
		throws Exception
	{
	
		return get(mapping , form, request , response);
	}	
	/**
	 *修改群相册
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GAlbumForm theForm = (GAlbumForm) form;
		

		ReturnMessage msg  = new ReturnMessage();
		
		service.update(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(theForm.getGroupID());
		theForm.setQryUserID(theForm.getUserID());
		
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping , form, request , response);
	}
	/**
	 *删除群相册
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GAlbumForm theForm = (GAlbumForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.delete(theForm,  msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(theForm.getGroupID());
		theForm.setQryUserID(theForm.getUserID());
		
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping, form, request, response);
	}	
	
	
}
